package com.by.common.crud;

import cn.hutool.core.map.MapUtil;
import com.by.common.bean.PageBean;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * @author wang
 * @version 版本号 ${}
 * @className BaseServiceImpl 默认实现
 * @日期 2024/9/4 16:08
 * @since jdk1.8
 */
public class BaseServiceImpl<T,M extends BaseMapper> implements BaseService<T>{
    
    @Autowired(required = false)
    protected M baseMapper;
    @Override
    public PageBean query(Map map) {
        int page= MapUtil.getInt(map,"page");
        int pageSize=MapUtil.getInt(map,"pageSize");
        //设置分页
        PageHelper.startPage(page,pageSize);//后面紧跟查询
        List<Map> list=baseMapper.query(map);
        
        //获取分页信息  将结果封装
        PageInfo info=new PageInfo(list);
        
        return new PageBean(page,pageSize,info.getTotal(),list);
    }
    
    @Override
    public int save(T entity) {
        return baseMapper.save(entity);
    }
    
    @Override
    public int update(T entity) {
        return baseMapper.update(entity);
    }
    
    @Override
    public int delete(Serializable id) {
        return baseMapper.delete(id);
    }
    
    @Override
    public T get(Serializable id) {
        return (T) baseMapper.get(id);
    }
}
